{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f2ad88ce",
   "metadata": {},
   "source": [
    "参考资料：  \n",
    "- https://numpy.org/doc/stable/reference/routines.matlib.html\n",
    "- https://www.runoob.com/numpy/numpy-matrix.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "87984353",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.21.0\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import numpy.matlib\n",
    "\n",
    "print(np.__version__)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b68d49f",
   "metadata": {},
   "source": [
    "#### Matrix的创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "0483de18",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "m <class 'numpy.matrix'> : \n",
      "[[1 2]\n",
      " [3 4]]\n",
      "\n",
      "\n",
      "m的形状：\n",
      "(2, 2)\n",
      "\n",
      "\n",
      "m的逆矩阵：\n",
      "[[-2.   1. ]\n",
      " [ 1.5 -0.5]]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "matrix(data[, dtype, copy])\n",
    "\"\"\"\n",
    "\n",
    "m = np.matrix('[1, 2; 3, 4]')\n",
    "print(\"m \"+repr(type(m))+\" : \")\n",
    "print(m)\n",
    "print('\\n')\n",
    "\n",
    "print(\"m的形状：\")\n",
    "print(m.shape)\n",
    "print('\\n')\n",
    "\n",
    "print(\"m的逆矩阵：\")\n",
    "print(m.getI())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "187a902e",
   "metadata": {},
   "source": [
    "另外还有2个matrix的函数别名  \n",
    "- mat(data[, dtype])\n",
    "- asmatrix(data[, dtype])\n",
    "\n",
    "看一眼源码就知道是怎么回事了。\n",
    "\n",
    "```\n",
    "def asmatrix(data, dtype=None):\n",
    "    return matrix(data, dtype=dtype, copy=False)\n",
    "\n",
    "mat = asmatrix\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "5ab0bd6a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 1, 2, 2],\n",
       "        [1, 1, 2, 2],\n",
       "        [3, 4, 7, 8],\n",
       "        [5, 6, 9, 0]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "matrix([[1, 2, 1, 2],\n",
       "        [3, 4, 3, 4],\n",
       "        [1, 2, 1, 2],\n",
       "        [3, 4, 3, 4]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\"\"\"\n",
    "bmat(obj[, ldict, gdict])\n",
    "\n",
    "- 增加了对元组，列表的支持。\n",
    "- 增强了对字符串的支持。\n",
    "\"\"\"\n",
    "\n",
    "A = np.mat('1 1; 1 1')\n",
    "B = np.mat('2 2; 2 2')\n",
    "C = np.mat('3 4; 5 6')\n",
    "D = np.mat('7 8; 9 0')\n",
    "\n",
    "\n",
    "display(np.bmat([[A, B], [C, D]]))\n",
    "\n",
    "A = np.array([[1, 2], [3, 4]])\n",
    "B = np.array([[5, 6], [7, 8]])\n",
    "display(np.bmat(\"A,A;A,A\", ldict={'A': A}, gdict={'A': B}))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c7dc989",
   "metadata": {},
   "source": [
    "#### numpy.matlib.empty()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "55b91929",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2.12199579e-314 8.01304298e+262]\n",
      " [1.73911107e-321 3.79442416e-321]]\n",
      "<class 'numpy.matrix'>\n",
      "[[-1656067513  1312848264]\n",
      " [-1968998200  1762508852]]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "numpy.matlib.empty() 函数返回一个新的矩阵，语法格式为：\n",
    "\"\"\"\n",
    "\n",
    "a = np.matlib.empty((2, 2))\n",
    "print(a)\n",
    "print(type(a))\n",
    "\n",
    "a = np.matlib.empty((2, 2), dtype=int)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "375b341a",
   "metadata": {},
   "source": [
    "#### numpy.matlib.zeros()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "96051ff6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 0.]\n",
      " [0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "numpy.matlib.zeros() 函数创建一个以 0 填充的矩阵。\n",
    "\"\"\"\n",
    "\n",
    "print (np.matlib.zeros((2,2)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03bd3b3f",
   "metadata": {},
   "source": [
    "#### numpy.matlib.ones()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8a9dfbbb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 1.]\n",
      " [1. 1.]]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "numpy.matlib.ones()函数创建一个以 1 填充的矩阵。\n",
    "\"\"\"\n",
    "\n",
    "print (np.matlib.ones((2,2)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c9f2ec6",
   "metadata": {},
   "source": [
    "#### numpy.matlib.eye()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "aee40305",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 0. 0. 0.]\n",
      " [0. 1. 0. 0.]\n",
      " [0. 0. 1. 0.]]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "numpy.matlib.eye() 函数返回一个矩阵，对角线元素为 1，其他位置为零。\n",
    "\"\"\"\n",
    "\n",
    "print (np.matlib.eye(n =  3, M =  4, k =  0, dtype =  float))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87adc79d",
   "metadata": {},
   "source": [
    "#### numpy.matlib.identity()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c6b0b47c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 0. 0. 0. 0.]\n",
      " [0. 1. 0. 0. 0.]\n",
      " [0. 0. 1. 0. 0.]\n",
      " [0. 0. 0. 1. 0.]\n",
      " [0. 0. 0. 0. 1.]]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "numpy.matlib.identity() 函数返回给定大小的单位矩阵。\n",
    "\n",
    "单位矩阵是个方阵，从左上角到右下角的对角线（称为主对角线）上的元素均为 1，除此以外全都为 0。\n",
    "\"\"\"\n",
    "\n",
    "# 大小为 5，类型位浮点型\n",
    "print (np.matlib.identity(5, dtype =  float))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbb2ea18",
   "metadata": {},
   "source": [
    "#### numpy.matlib.repmat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "480c3b2e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1 1]\n",
      " [1 1 1]]\n",
      "[[0 1 2 3 0 1 2 3]\n",
      " [0 1 2 3 0 1 2 3]]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "numpy.matlib.repmat() 函数创建一个重复0维-2维数组或矩阵MxN次的矩阵。\n",
    "\"\"\"\n",
    "\n",
    "a0 = np.array(1)\n",
    "print(np.matlib.repmat(a0, 2, 3))\n",
    "\n",
    "a1 = np.arange(4)\n",
    "print(np.matlib.repmat(a1, 2, 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88fef044",
   "metadata": {},
   "source": [
    "#### numpy.matlib.rand()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "643aaea7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.73862067 0.89130479 0.04613588]\n",
      " [0.57907829 0.616952   0.60338401]\n",
      " [0.70272513 0.58623063 0.90562988]]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "numpy.matlib.rand() 函数创建一个给定大小的矩阵，数据是随机填充的。\n",
    "\"\"\"\n",
    "\n",
    "print (np.matlib.rand(3,3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12d069f1",
   "metadata": {},
   "source": [
    "#### numpy.matlib.randn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "a8594750",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.44057013  2.14173308 -1.01978141]\n",
      " [ 0.32216631  0.74241967  0.49214318]\n",
      " [ 0.08803868 -1.87864349  1.90468375]]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "numpy.matlib.randn() 函数创建一个给定大小的矩阵，数据是正态分布随机填充的。\n",
    "\"\"\"\n",
    "\n",
    "print (np.matlib.randn(3,3))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
